package FriendTable.Gui;
/*
更新A
 */

import FriendTable.Friend;
import FriendTable.FriendDAO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdatePanel extends JPanel {
    private InputPanel idInput;
    private InputPanel nameInput;
    private InputPanel genderInput;
    private InputPanel ageInput;
    private InputPanel favorabilityInput;
    private InputPanel evaluationInput;
    private JButton updateButton;
    private JTextArea resultArea;

    public UpdatePanel() {
        setLayout(new GridLayout(8, 2));

        idInput = new InputPanel("ID:", null);
        add(idInput);

        nameInput = new InputPanel("姓名:", null);
        add(nameInput);

        String[] genders = {"男", "女"};
        genderInput = new InputPanel("性别:", genders);
        add(genderInput);

        ageInput = new InputPanel("年龄:", null);
        add(ageInput);

        favorabilityInput = new InputPanel("好感度:", null);
        add(favorabilityInput);

        evaluationInput = new InputPanel("评价:", null);
        add(evaluationInput);

        updateButton = new JButton("更新");
        add(updateButton);

        resultArea = new JTextArea();
        add(resultArea);

        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int id;
                try {
                    id = Integer.parseInt(idInput.textField.getText());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "请输入有效的ID！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String name = nameInput.textField.getText();
                if (name.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "请输入姓名！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String gender = (String) genderInput.comboBox.getSelectedItem();
                int age;
                try {
                    age = Integer.parseInt(ageInput.textField.getText());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "请输入有效的年龄！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                int favorability;
                try {
                    favorability = Integer.parseInt(favorabilityInput.textField.getText());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "请输入有效的好感度！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String evaluation = evaluationInput.textField.getText();

                Friend friend = new Friend(id, name, gender.charAt(0), age, favorability, evaluation);
                FriendDAO.updateFriend(friend);
                resultArea.setText("更新成功！");
                FriendGUI.refreshFriendsList();
            }
        });
    }
}